package test;


import java.util.Iterator;

import com.duniptech.engine.core.modeling.Atomic;
import com.duniptech.engine.core.modeling.Message;
import com.duniptech.engine.core.modeling.api.IMessage;

public class Processor extends Atomic {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Ports
	public static final String in = "in";
	public static final String out = "out";
	
	// State	
	protected Job currentJob;
	protected double processingTime;

	public Processor(String name, double processingTime){
		super(name);
		addInport(Processor.in);
		addOutport(Processor.out);
		currentJob = null;
		this.processingTime = processingTime;
	}

	public Processor() {
		this("Processor",10.0);
	}

	public void deltint() {
		//System.out.println("Start: " + this.getClass().getName() + "::deltint()");
		setSigma(INFINITY);
		currentJob = null;
		//System.out.println("End: " + this.getClass().getName() + "::deltint()");
	}

	public void  deltext(double e, IMessage x) {
		//System.out.println("Start: " + this.getClass().getName() + "::deltext()");
		//System.out.println("deltext");
		setSigma(getSigma()-e);
		if (getSigma() == INFINITY) {
			Iterator<?> itr = x.getValuesOnPort(Processor.in).iterator();
			while(itr.hasNext()) {
				currentJob = (Job)itr.next();
				setSigma(processingTime);
			}
		}
		//System.out.println("End: " + this.getClass().getName() + "::deltext()");
	}

	public IMessage lambda() {
		//System.out.println("Start: " + this.getClass().getName() + "::lambda()");
		IMessage msg = new Message();
		if(currentJob!=null) msg.add(Processor.out, currentJob);
		//System.out.println("End: " + this.getClass().getName() + "::lambda()");
		return msg;
	}

}
